package com.nc.cloud.admin.rest;

import com.alibaba.nacos.common.util.UuidUtils;
import com.nc.cloud.admin.biz.MenuBiz;
import com.nc.cloud.admin.biz.RoleMenuBiz;
import com.nc.cloud.admin.entity.Menu;
import com.nc.cloud.admin.entity.RoleMenu;
import com.nc.cloud.common.context.BaseContextHandler;
import com.nc.cloud.common.msg.BaseResponse;
import com.nc.cloud.common.msg.ObjectRestResponse;
import com.nc.cloud.common.rest.BaseController;
import com.nc.cloud.admin.biz.RoleBiz;
import com.nc.cloud.admin.entity.Role;
import com.sun.prism.impl.BaseContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("role")
public class RoleController extends BaseController<RoleBiz, Role> {

    @Autowired
    RoleBiz roleBiz;

    @Autowired
    RoleMenuBiz roleMenuBiz;

    @PostMapping (value = "/roleFullMsg")
    @ResponseBody
    @Transactional
    public ObjectRestResponse<Role> addRoleFullMsg(@RequestBody HashMap params){


        Role role ;
        List<HashMap<String,Object>>  roleMenu ;

        if(params.get("code") == null || params.get("name") == null || params.get("menus") == null){
            //return new BaseResponse(200,"信息填写错误");
        }

        role = new Role();//uid,code,name,type,status,company_code,create_user_code,create_time
        role.setUid(UuidUtils.generateUuid());
        role.setCode(params.get("code").toString());
        role.setName(params.get("name").toString());
        role.setType("SYS");
        role.setStatus("1");
        role.setCompanyCode("");
        role.setCreateUserCode(BaseContextHandler.getCode());
        role.setCreateTime(new Date());


        role.setCreateUserCode(BaseContextHandler.getCode());



        roleMenu = (List<HashMap<String, Object>>) params.get("menus");

        roleMenu.forEach(item->{
            item.put("roleCode",role.getCode());
            item.put("menuCode",item.get("code"));
            item.put("status","1");
            item.put("createUserCode", BaseContextHandler.getCode());
            item.put("createTime", new Date());

        });

        roleBiz.insertFullMsg(role,roleMenu);

        return new ObjectRestResponse<>().data(role);
    }

}